<template>
  <div class="manage">
    <el-dialog :title="operateType == 'add' ? '新增用户' : '更新用户'" :visible.sync="isShow">
      <common-form :formLable="operateFormLabel" :form="operateForm" :inline="false"> </common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="add">+ 新增</el-button>
      <common-form :formLable="formLable" :form="searchForm" :inline="true" @search="blur">
        <el-button type="primary">搜索</el-button>
      </common-form>
    </div>
    <!-- 子组件向父组件通信以后 @changePage的事件可以 当做程序调用一次getList  -->
    <common-table :tableData="tableData" :tableLabel="tableLabel" :config="config" @changePage="getList" @edit="editUser" @del="delUser"></common-table>
  </div>
</template>

<script>
import CommonForm from '../../components/CommonForm.vue'
import CommonTable from '../../components/CommonTable.vue'

export default {
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      // 由于新增个编辑是一样的 但是titel不一样 所以需要定义一个数据源
      operateType: 'add',
      isShow: false,
      operateForm: {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',
      },
      operateFormLabel: [
        {
          model: 'name',
          label: '姓名',
        },
        {
          model: 'age',
          label: '年龄',
        },
        {
          model: 'sex',
          label: '性别',
          // 性别是一个下拉框 所以要传一个进去
          type: 'select',
          opts: [
            {
              label: '男',
              value: 1,
            },
            {
              label: '女',
              value: 0,
            },
          ],
        },
        {
          model: 'birth',
          label: '出生日期',
          // 是一个日期选择器,所以传一个进去
          type: 'date',
        },
        {
          model: 'addr',
          label: '地址',
        },
      ],
      searchForm: {
        keyword: '',
      },
      formLable: [
        {
          model: 'keyword',
          label: '',
        },
      ],
      tableData: [],
      tableLabel: [
        {
          prop: 'name',
          label: '姓名',
        },
        {
          prop: 'age',
          label: '年龄',
        },
        {
          prop: 'sexLabel',
          label: '性别',
        },
        {
          prop: 'birth',
          label: '出生日期',
          // 列宽
          // width: 200,
        },
        {
          prop: 'addr',
          label: '地址',
          // width: 400,
        },
      ],
      config: {
        page: 1,
        total: 30, // 总页数
        loading: false,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.config.loading = true
      this.$http
        .get('/api/user/getUser', {
          params: {
            page: this.config.page,
          },
        })
        .then((res) => {
          this.tableData = res.data.list.map((item) => {
            item.sexLabel = item.sex === 0 ? '女' : '男'
            return item
          })
          this.config.total = res.data.count
          this.config.loading = false
        })
    },
    editUser(row) {
      this.operateType = 'edit'
      this.isShow = true
      this.operateForm = row
    },
    confirm() {
      if (this.operateType === 'edit') {
        this.$http.post('/api/user/edit', this.operateForm).then((res) => {
          console.log(res.data)
          this.isShow = false
          this.getList()
        })
      } else {
        this.$http.post('/api/user/add', this.operateForm).then((res) => {
          console.log(res.data)
          this.isShow = false
          this.getList()
        })
      }
    },
    delUser(row) {
      this.$confirm('确定删除此行数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          let id = row.id
          this.$http
            .get('/user/del', {
              params: { id },
            })
            .then((res) => {
              console.log(res.data)
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.getList()
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    add() {
      this.operateType === ''
      this.isShow = true
    },
    blur(item) {
      console.log(item)
      this.config.loading = true
      this.$http
        .get('/api/user/getUser', {
          params: {
            name: item,
          },
        })
        .then((res) => {
          this.tableData = res.data.list.map((item) => {
            item.sexLabel = item.sex === 0 ? '女' : '男'
            return item
          })
          this.config.total = res.data.count
          this.config.loading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/assets/scss/common.scss';
</style>
